package test.orm;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import zdy.web.bean.User;

public class tehibernate {
	 private static SessionFactory sessionFactory;

	    static {
	        Configuration cfg = new Configuration();
	        cfg.configure("hibernate.cfg.xml"); // 读取指定的主配置文件
	        sessionFactory = cfg.buildSessionFactory(); // 根据配置生成Session工厂
	    }
	    @Before
		public void setUp() throws Exception {
		}

		@After
		public void tearDown() throws Exception {
		}

	    @Test
	    public void testSave() {
	        User user = new User();
	        user.setName("张三");

	        // 保存
	        Session session = sessionFactory.openSession(); // 打开一个新的Session
	        Transaction tx = session.beginTransaction(); // 开启事务

	        session.save(user);

	        tx.commit(); // 提交事务
	        session.close(); // 关闭Session，释放资源(不一定是真正的关闭)
	    }

	    @Test
	    public void testGet() {
	        Session session = sessionFactory.openSession();
	        Transaction tx = session.beginTransaction();

	        User user = (User) session.get(User.class, 1); // 获取？
	        System.out.println(user);

	        tx.commit();
	        session.close();
	    }
}
